<?php
/**
 * Created by PhpStorm.
 * User: zhongbaitong
 * Date: 2020/1/10
 * Time: 14:39
 */

namespace app\im\model;

/**
 * @method belongsTo(string $string)
 * @method hasOne(string $string, string $string1, string $string2)
 */
class ChatLists extends ImBase
{

    /**
     * 一对一获取最新一条记录
     * @return mixed
     */
    public function chat()
    {
        return $this->hasOne("Chat", "root_id", "root_id");
    }

    /**
     * 一对一获取用户信息
     * @return mixed
     */
    public function user()
    {
        return $this->belongsTo("user");
    }

    /**
     * 获取聊天列表数据
     * @param $params
     * @return array
     */
    public static function getLists($params)
    {
        if (!$user_id = $params["user_id"])
            return [];

        // 所在的ids、root_ids
        $root_ids = self::getRootIDs($user_id);
        $self_id = self::where(compact("user_id"))
            ->where("is_self", 1)
            ->value("id");

        // 除去 self_id  (for not in)
        $ids = array_diff(array_keys($root_ids), [$self_id]);

        $data = ChatLists::order("update_time desc")
            ->lists($params)
            ->with(["chat" => function ($q) {
                $q->scope("content");
            }, "user"])
            ->where("root_id", "in", $root_ids)
            ->where("id", "not in", $ids)
            ->where("is_del", "neq", 1)
            ->all();

        return $data;
    }


    // 获取 [ id=>root_id,.. ]
    public static function getRootIDs($user_id)
    {

        $result = self::where(compact("user_id"))
            ->column("id,root_id");

        return $result;
    }

    // 获取两人同一房号
    public static function getRootID($user_id, $obj_uid)
    {
        if ($user_id == $obj_uid) {
            $root_id = self::where([
                "user_id" => $user_id,
                "is_self" => 1
            ])->value("root_id");
        } else {
            $user_root_ids = self::getRootIDs($user_id);
            $root_id = self::where("user_id", $obj_uid)
                ->where("root_id", "in", $user_root_ids)
                ->value("root_id");
        }
        return (int)$root_id;
    }

    /**
     * 未读数 + 1
     * @param $user_id
     * @param $root_id
     * @return int|true
     * @throws \think\Exception
     */
    public static function incUnreadNum($user_id, $root_id)
    {
        $result = self::where("root_id", $root_id)
            ->where("user_id", $user_id)
            ->where("is_self", 0)
            ->setInc("unread_num");

        return $result;
    }

    // 设置消息已读
    public static function setReaded($user_id, $root_id)
    {

        $result = self::where("user_id", "neq", $user_id)
            ->where("root_id", $root_id)
            ->setField("unread_num", 0);

        return $result;
    }

    // 未读用户头像列表
    public static function getUnReadInfo($user_id)
    {
        // 房间号
        $root_ids = self::getRootIDs($user_id);

        $query = ChatLists::where("root_id", "in", $root_ids)
            ->where("user_id", "neq", $user_id)
            ->where("unread_num", ">", 0);

        // 未读用户 uids
        $obj_uids = $query->column("user_id");

        // 未读数
        $chat_count = $query->sum("unread_num");

        return compact("obj_uids", "chat_count");
    }

}